<?php

	/**
     * Redirect
     *
	 * Plugin for front end URL redirection
	 *
	 * @package      Plugins
	 * @subpackage   Redirect
     * 
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
	 */
    class Plugin_Redirect implements Core_Plugin_Interface
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Plugin configuration values
         *
         * @var Zend_Config
         */
        protected $_config = null;

		/**
		 * Zend_Registry object
		 *
		 * @var Zend_Registry
		 */
		protected $_registry = null;

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Returns dependencies list.
         *
         * @return      array
         */
        public function getDependencies()
        {
            return array();
        }

        /**
         * Contructor
         *
         * throws       Core_Exception
		 */
        public function __construct()
        {
            $this->_registry = Zend_Registry::getInstance();

            $configuration   = Core_Plugin_Config::getInstance('config.ini.php', $this->_registry['section']);
            $this->_config   = $configuration->{'redirect'};

            if (!empty($this->_config->requires)) {
                $required = explode(',', $this->_config->requires);

                foreach ($required as $module) {
                    if (!array_key_exists($module, $this->_registry['modules'])) {
                        throw new Core_Exception('Missing plugin dependency: ' . $module);
                    }
                }
            }
        }

		/**
		 * Destructor
		 */
		public function __destruct()
		{
			unset($this);
		}

        /**
         * @param       array       $params : plugin parameters for the current action
         * @return      Core_Selector_Action|null    if action should change
         */
        public function beforeAction($params)
        {
            $redirectRequired    = isset($params['redirect.required']) ? ($params['redirect.required'] == true) : false;

            if (!$redirectRequired) {
                return null;
            }

            $coordinator    = Core_Controller_Coordinator::getInstance();
            $request        = $coordinator->getRequest();
            $url            = $request->getParam($this->_config->detection->url->param);

            if ($url === null) {
                $url = '';
            }

            // Tries to retrieve page object
            $page = DbPage::match($url .'/');

            if ($page === null) {
                // Raise 404 error
                return new Core_Selector_GenericAction('Base~Index:notFound');
            }

            $module = null;
            $action = null;
            list($module, $action) = explode('~', $page['page']->page_method);

            // Sets new request params
            $params               = $page['params'];
            $params['pageObject'] = $page['page'];

            $request->setParams(array_merge($request->getParams(), $params));
            return new Core_Selector_Action($request->getType(), $module, $action);
        }

        public function beforeOutput() { }
        public function afterProcess() { }
    }
